package com.xasz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xasz.entity.Activity;
import com.xasz.entity.Weight;
import com.xasz.mapper.ActivityMapper;
import com.xasz.mapper.WeightMapper;
import com.xasz.service.IWeightService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xasz.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cai
 * @since 2022-08-03
 */
@Service
public class WeightServiceImpl extends ServiceImpl<WeightMapper, Weight> implements IWeightService {

    @Autowired
    WeightMapper weightMapper;

    @Override
    public void setWeight(Integer activityId,List<Weight> weightList,Integer Uid) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("activity_id",activityId)).getOwner().equals(Uid)){
            throw new DeleteException("你没有权限修改该活动维度信息");
        }
        //循环
        int i=0;
        for (Weight weight:weightList){
            weight.setActivityId(activityId);
            weight.setScoreX(++i);
            //数量超过6
            if (i>6){
                throw new ExceedMaxException("维度超过最大数量6");
            }
            //查询一下避免重复覆盖-确保权重没有被设置
            QueryWrapper<Weight> wrapper=new QueryWrapper<Weight>()
                    .eq("activity_id",activityId)
                    .eq("name",weight.getName());
            if (weightMapper.selectOne(wrapper)!=null){
                throw new RepeatException("维度已经存在:"+weight.getName());
            }
            //添加
            weightMapper.insert(weight);
        }


















        //循环组合评分项和对应权重
//        for (String name:names){
//            weight.setName(name);
//            weight.setWeight(weights[i++]);
//            weight.setScoreX(i);
//            //数量超过6
//            if (i>=7){
//                throw new ExceedMaxException("维度超过最大数量6");
//            }
//            //查询一下避免重复覆盖-确保权重没有被设置
//            QueryWrapper<Weight> wrapper=new QueryWrapper<Weight>()
//                    .eq("activity_id",activityId)
//                    .eq("name",name);
//            if (weightMapper.selectOne(wrapper)!=null){
//                throw new RepeatException("维度已经存在:"+name);
//            }
//            //添加
//            weightMapper.insert(weight);
//        }
    }

    @Override
    public void detectWeight(Weight weight,Integer Uid) {
        Integer activityId= weight.getActivityId();
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("activity_id",activityId)).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限修改该活动维度信息");
        }
        QueryWrapper<Weight> wrapper=new QueryWrapper<Weight>()
                .eq("activity_id",activityId)
                .eq("score_x",weight.getScoreX());
        weightMapper.delete(wrapper);
    }

    @Override
    public List<Weight> getWeight(Integer activityId) {
        QueryWrapper<Weight> wrapper=new QueryWrapper<Weight>()
                .eq("activity_id",activityId);
        return weightMapper.selectList(wrapper);
    }
    @Autowired
    ActivityMapper activityMapper;
    @Override
    public void upDateWeight(Weight weight,Integer Uid) {
        Integer activityId=weight.getActivityId();
        Integer scoreX=weight.getScoreX();
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>().eq("activity_id",activityId)).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限修改该活动维度信息");
        }
        //查询是否存在
        QueryWrapper<Weight> wrapper=new QueryWrapper<Weight>()
                .eq("activity_id",activityId)
                .eq("score_x",scoreX);
        if (weightMapper.selectOne(wrapper)==null){
            throw new NotNxistException("维度不存在");
        }
        //查询是否重复
        QueryWrapper<Weight> wrapperName=new QueryWrapper<Weight>()
                .eq("activity_id",activityId)
                .eq("name",weight.getName());
        if (weightMapper.selectOne(wrapperName)!=null){
            throw new NotNxistException("维度名称重复");
        }
        //修改数据
        UpdateWrapper<Weight> updateWrapper=new UpdateWrapper<Weight>()
                .eq("activity_id",activityId)
                .eq("score_x",scoreX);
        if (weightMapper.update(weight,updateWrapper)!=1){
            throw new UpdateException("修改维度时出现错误");
        }
    }
}
